package com.real.fact.web.sale.application;

import org.springframework.validation.Errors;

import com.globant.katari.core.application.ValidatableCommand;
import com.real.fact.web.sale.domain.SaleRepository;
import com.real.fact.web.sale.domain.Seller;

public class EditSellerCommand implements ValidatableCommand<Void>{

  private String name;
  
  private String password;
  
  private long sellerId;
  
  private SaleRepository saleRepository;
  
  public EditSellerCommand(SaleRepository saleRepository) {
    super();
    this.saleRepository = saleRepository;
  }

  public Void execute() {
    Seller seller = null;
    if (sellerId != 0) {
      seller = saleRepository.findSellerById(sellerId);
      seller.setName(name);
      seller.setPassword(password);
    } else {
      seller = new Seller(name, password);
    }
    saleRepository.save(seller);
    return null;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public long getSellerId() {
    return sellerId;
  }

  public void setSellerId(long sellerId) {
    this.sellerId = sellerId;
  }

  public void init() {
    if (sellerId > 0) {
      Seller seller = saleRepository.findSellerById(sellerId);
      this.name = seller.getName();
      this.password = seller.getPassword();
    }
  }

  public void validate(Errors errors) {
    // TODO Auto-generated method stub
    
  }

}
